﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HZY.Logic.Sys
{
    using System.Linq;
    using System.Threading.Tasks;
    using System.Collections;
    using HZY.DbExtensions;
    using HZY.Models.Sys;
    using HZY.Models;
    using HZY.Toolkit;
    using HZY.DTO.Sys;
    using HZY.Logic.Core;
    using HzySql.Interface;

    public class Sys_AppLogLogic : LogicBase
    {

        public Sys_AppLogLogic(HzySqlDbContext dbContext)
            : base(dbContext)
        {


        }

        #region CURD 基础

        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<TableViewModel> FindListAsync(int Page, int Rows, Sys_AppLog Search, DateTime? StartTime, DateTime? EndTime)
        {
            var query = db.Query<Sys_AppLog>()
                .Join<Sys_User>(w => w.t1.AppLog_UserID == w.t2.User_ID)
                .Where(w => w.t1.AppLog_Api.Contains(Search.AppLog_Api), !string.IsNullOrEmpty(Search?.AppLog_Api))
                .Where(w => w.t1.AppLog_IP.Contains(Search.AppLog_IP), !string.IsNullOrEmpty(Search?.AppLog_IP))
                .Where(w => w.t1.AppLog_CreateTime >= StartTime & w.t1.AppLog_CreateTime <= EndTime, StartTime != null && EndTime != null)
                .Select(w => new
                {
                    w.t1.AppLog_IP,
                    w.t1.AppLog_Api,
                    w.t1.AppLog_Form,
                    w.t1.AppLog_FormBody,
                    w.t1.AppLog_QueryString,
                    操作人 = w.t2.User_Name,
                    w.t1.AppLog_CreateTime,
                    _ukid = w.t1.AppLog_ID
                })
                .OrderByDesc(w => w.t1.AppLog_CreateTime)
                .TakePage(Page, Rows, out int TotalCount)
                ;

            return await this.db.AsTableViewModelAsync(query, Page, Rows, TotalCount);
        }

        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> SaveAsync(Sys_AppLog model)
        {
            if (model.AppLog_ID == Guid.Empty)
            {
                model.AppLog_ID = await db.Insert(model).SaveAsync<Guid>();
            }
            else
            {
                await db.UpdateById(model).SaveAsync();
            }

            return model.AppLog_ID;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Keys"></param>
        /// <returns></returns>
        public async Task DeleteAsync(List<Guid> Ids)
        {
            this.iSqlContexts = new List<ISqlContext>();

            foreach (var item in Ids)
            {
                db.Delete<Sys_AppLog>(w => w.t1.AppLog_ID == item).ToSqlContext(this.iSqlContexts);
            }

            //此函数用于批量 sql 操作
            await db.ExecuteBatchAsync(this.iSqlContexts);
        }

        /// <summary>
        /// 加载表单 数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<object> LoadFormAsync(Guid Id)
        {
            var Model = await db.FindByIdAsync<Sys_AppLog>(Id);
            var _Sys_User = await db.FindByIdAsync<Sys_User>(Model.AppLog_UserID);

            return new { Id, Model, _Sys_User.User_Name };
        }


        #endregion













    }
}
